package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.modular.business.pojo.SubFileInfoRequest;
import cn.stylefeng.guns.modular.business.service.SubFileInfoService;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制器
 *
 * @author zhangyaowen
 * @date 2021/08/19 16:49
 */
@RestController
@ApiResource(name="文件controller")
public class SubFileInfoController {

    @Resource
    private SubFileInfoService subFileInfoService;
    @Resource
    private LoginUserApi loginUserApi;


    /**
     * 添加
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @PostResource(name = "添加", path = "/subFileInfo/add")
    public ResponseData add(@RequestBody @Validated(SubFileInfoRequest.add.class) SubFileInfoRequest subFileInfoRequest) {
        subFileInfoService.add(subFileInfoRequest);
        return new SuccessResponseData();
    }

    /**
     * 删除
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @PostResource(name = "删除", path = "/subFileInfo/delete")
    public ResponseData delete(@RequestBody @Validated(SubFileInfoRequest.delete.class) SubFileInfoRequest subFileInfoRequest) {
        subFileInfoService.delete(subFileInfoRequest);
        return new SuccessResponseData();
    }

    /**
     * 编辑
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @PostResource(name = "编辑", path = "/subFileInfo/edit")
    public ResponseData edit(@RequestBody @Validated(SubFileInfoRequest.edit.class) SubFileInfoRequest subFileInfoRequest) {
        subFileInfoService.edit(subFileInfoRequest);
        return new SuccessResponseData();
    }

    /**
     * 查看详情
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @GetResource(name = "查看详情", path = "/subFileInfo/detail")
    public ResponseData detail(@Validated(SubFileInfoRequest.detail.class) SubFileInfoRequest subFileInfoRequest) {
        return new SuccessResponseData(subFileInfoService.detail(subFileInfoRequest));
    }

    /**
     * 获取列表
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @GetResource(name = "获取列表", path = "/subFileInfo/list")
    public ResponseData list(SubFileInfoRequest subFileInfoRequest) {
        return new SuccessResponseData(subFileInfoService.findList(subFileInfoRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @GetResource(name = "分页查询", path = "/subFileInfo/page")
    public ResponseData page(String fileType,String name) {
        SubFileInfoRequest subFileInfoRequest = new SubFileInfoRequest();
        Long fileTypes = Long.parseLong(fileType);
        subFileInfoRequest.setFileType(fileTypes);
        subFileInfoRequest.setFileOriginName(name);
        return new SuccessResponseData(subFileInfoService.findPage(subFileInfoRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @GetResource(name = "图库分页查询", path = "/subFileInfo/image")
    public ResponseData image(String name) {
        boolean superAdmin = loginUserApi.getSuperAdminFlag();
        if(superAdmin) {
            SubFileInfoRequest subFileInfoRequest = new SubFileInfoRequest();
            Long fileTypes = 1l;
            subFileInfoRequest.setFileType(fileTypes);
            subFileInfoRequest.setFileOriginName(name);
            return new SuccessResponseData(subFileInfoService.findImage(subFileInfoRequest));
        }else{
            return new SuccessResponseData("201","暂无权限，请联系管理员！","");
        }
    }

    /**
     * 获取列表（带分页）通过id
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @GetResource(name = "分页查询通过id", path = "/subFileInfo/pageById")
    public ResponseData pageById(String fileType,String id) {
        SubFileInfoRequest subFileInfoRequest = new SubFileInfoRequest();
        Long fileTypes = Long.parseLong(fileType);
        subFileInfoRequest.setFileType(fileTypes);
        Long fileId = Long.parseLong(id);
        subFileInfoRequest.setId(fileId);
        return new SuccessResponseData(subFileInfoService.findPageById(subFileInfoRequest));
    }
    /**
     * 上传文件
     *
     * @author zhangyaowen
     * @date 2021/08/19 16:49
     */
    @PostResource(name = "上传文件", path = "/subFileInfo/upload")
    public ResponseData upload(@RequestPart("files") MultipartFile [] files) {
        List list = new ArrayList();
        for( MultipartFile file : files) {
            Long fileId = subFileInfoService.uploadFile(file);
            list.add(fileId);
        }
        return new SuccessResponseData(list);
    }


    /**
     * 文件预览
     *
     * @author zhangyaowen
     * @date 2021/8/24 13:36
     */
    @GetResource(path ="/subFileInfo/preview")
    public void preview(String id, HttpServletResponse response) {
        SubFileInfoRequest  subFileInfoRequest = new SubFileInfoRequest();
        Long idlong = Long.parseLong(id);
        subFileInfoRequest.setId(idlong);
        subFileInfoService.preview(subFileInfoRequest, response);
    }


    /**
     * 文件缩略图
     *
     * @author zhangyaowen
     * @date 2021/8/24 13:36
     */
    @GetResource(path ="/subFileInfo/rarPreview")
    public void rarPreview(String id, HttpServletResponse response) {
        SubFileInfoRequest  subFileInfoRequest = new SubFileInfoRequest();
        Long idlong = Long.parseLong(id);
        subFileInfoRequest.setId(idlong);
        subFileInfoService.rarPreview(subFileInfoRequest, response);

    }



    @GetResource(path ="/subFileInfo/assFileByValue")
    public ResponseData assFileByValue(String value ,String fileType) {
        return new SuccessResponseData(subFileInfoService.assFileByValue(value, fileType));
    }


}